package com.ruoyi.project.AnswerQuestion.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.AnswerQuestion.domain.TbQuestion;
import com.ruoyi.project.AnswerQuestion.domain.TbQuestionAnswer;
import com.ruoyi.project.AnswerQuestion.service.ITbQuestionAnswerService;
import com.ruoyi.project.AnswerQuestion.service.ITbQuestionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author lvyongwei
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/system/answer")
public class TbQuestionAnswerController extends BaseController
{
    private String prefix = "system/answer";

    @Autowired
    private ITbQuestionAnswerService tbQuestionAnswerService;

    @Autowired
    private ITbQuestionService tbQuestionService;

    //@RequiresPermissions("system:answer:view")
    @GetMapping()
    public String answer(Long questionId,ModelMap modelMap)
    {
        modelMap.addAttribute("questionId",questionId);
        //获取问题内容
        TbQuestion tbQuestion = tbQuestionService.selectTbQuestionById(questionId);
        modelMap.addAttribute("question",tbQuestion);
        TbQuestionAnswer tbQuestionAnswer = new TbQuestionAnswer();
        tbQuestionAnswer.setQuestionId(questionId);
        //获取所有问题
        List<TbQuestionAnswer> list = tbQuestionAnswerService.selectTbQuestionAnswerList(tbQuestionAnswer);
        modelMap.addAttribute("answers",list);
        //获取当前用户id
        modelMap.addAttribute("userId",getSysUser().getUserId());
        return prefix + "/answer";
    }

    @GetMapping("/lookAnswer")
    public String lookAnswer(Long questionId,ModelMap modelMap)
    {
        modelMap.addAttribute("questionId",questionId);
        //获取问题内容
        TbQuestion tbQuestion = tbQuestionService.selectTbQuestionById(questionId);
        modelMap.addAttribute("question",tbQuestion);
        TbQuestionAnswer tbQuestionAnswer = new TbQuestionAnswer();
        tbQuestionAnswer.setQuestionId(questionId);
        //获取所有问题
        List<TbQuestionAnswer> list = tbQuestionAnswerService.selectTbQuestionAnswerList(tbQuestionAnswer);
        modelMap.addAttribute("answers",list);
        //获取当前用户id
        modelMap.addAttribute("userId",getSysUser().getUserId());
        return prefix + "/answer2";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    //@RequiresPermissions("system:answer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbQuestionAnswer tbQuestionAnswer,ModelMap modelMap)
    {
        startPage();
        List<TbQuestionAnswer> list = tbQuestionAnswerService.selectTbQuestionAnswerList(tbQuestionAnswer);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    //@RequiresPermissions("system:answer:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbQuestionAnswer tbQuestionAnswer)
    {
        List<TbQuestionAnswer> list = tbQuestionAnswerService.selectTbQuestionAnswerList(tbQuestionAnswer);
        ExcelUtil<TbQuestionAnswer> util = new ExcelUtil<TbQuestionAnswer>(TbQuestionAnswer.class);
        return util.exportExcel(list, "answer");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    //@RequiresPermissions("system:answer:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbQuestionAnswer tbQuestionAnswer)
    {
        Long userId = getSysUser().getUserId();
        tbQuestionAnswer.setQuestionAnswerUser(userId);
        return toAjax(tbQuestionAnswerService.insertTbQuestionAnswer(tbQuestionAnswer));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{questionAnswerId}")
    public String edit(@PathVariable("questionAnswerId") Long questionAnswerId, ModelMap mmap)
    {
        TbQuestionAnswer tbQuestionAnswer = tbQuestionAnswerService.selectTbQuestionAnswerById(questionAnswerId);
        mmap.put("tbQuestionAnswer", tbQuestionAnswer);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    //@RequiresPermissions("system:answer:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbQuestionAnswer tbQuestionAnswer)
    {
        return toAjax(tbQuestionAnswerService.updateTbQuestionAnswer(tbQuestionAnswer));
    }

    /**
     * 删除【请填写功能名称】
     */
    //@RequiresPermissions("system:answer:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbQuestionAnswerService.deleteTbQuestionAnswerByIds(ids));
    }
}
